package moldat;

import java.io.IOException;
import java.io.Writer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import moldat.utilities.Exporter;

/**
 * Diese Klasse verarbeitet alle Anfragen, die von der 'ExportPage' gestellt
 * werden. Diese Seite wird ueber den Link 'Export' geoeffnet. Es wird eine
 * Instanz der Klasse 'Exporter' benutzt, um den Datenbank-Inhanlt zu
 * extrahieren und in einen String umzuwandeln.
 *
 * @see moldat.Protein
 * @author Marc Zoeller
 */
public class ExportServlet extends HttpServlet {

    /**
     * Verarbeitet alle Post- und Get-Requests an dieses Servlet.
     *
     * @param request HTML-Request des Clients
     * @param response HTML-Response des Servers
     * @throws ServletException
     * @throws IOException
     */
    private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("title", "Export");

        String protein = request.getParameter("protein");
        String gene = request.getParameter("gene");
        String all = request.getParameter("all");
        if ((protein != null && (!protein.equals("") || !gene.equals(""))) || all != null) {
            response.setContentType("text/plain");
            Exporter export = Exporter.getInstance();
            String content;
            try {
                if (!protein.equals(""))
                    content = export.exportProtein(request.getParameter("protein"));
                else {
                    if (!gene.equals(""))
                        content = export.exportGene(request.getParameter("gene"));
                    else
                        content = export.exportAll();
                }
                //Erzeugt die neue HTML-Seite
                Writer out = response.getWriter();
                out.write(content);
                out.flush();
            } catch (Exception ex) {
                System.out.println("Error while exporting: " + ex.getMessage());
                request.setAttribute("error", ex.getMessage());
            }
        }
        else {
            request.setAttribute("title", "Export");
            getServletContext().getNamedDispatcher("ExportPage").forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
